package com.sneakxy.mybase4j.generator.jdbc;

import org.apache.commons.lang3.StringUtils;

/**
 * Mysql方言
 * @author 潜行的青衣
 * @see
 */
public class MysqlDialect implements Dialect {

	private String database;
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.generator.jdbc.Dialect#getTablesSql()
	 */
	@Override
	public String getTablesSql() {
		return "select table_name from information_schema.tables where table_schema='"+database+"' and table_type='base table'";
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.generator.jdbc.Dialect#setUrl(java.lang.String)
	 */
	@Override
	public void setUrl(String url) {
		int index = StringUtils.lastIndexOf(url, "/");
		int lastIndex = StringUtils.indexOf(url, "?");
		if(lastIndex > -1) {
			this.database = StringUtils.substring(url, index + 1, lastIndex);
		} else {
			this.database = StringUtils.substring(url, index + 1);
		}
	}

	public String getDatabase() {
		return database;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.generator.jdbc.Dialect#getSelectNone(java.lang.String)
	 */
	@Override
	public String getSelectNone(String table) {
		return "select * from " + table + " where 1!=1";
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.generator.jdbc.Dialect#getCatalog()
	 */
	@Override
	public String getCatalog() {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.generator.jdbc.Dialect#getSchema()
	 */
	@Override
	public String getSchema() {
		return database;
	}
	
}
